<?php 

#	Fastfoo - CMS developer under CakePHP / http://fast-foo.com
#	Copyright (C) 2010  Antonio Irlandés García

#	This program is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.

#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.

#	You should have received a copy of the GNU General Public License
#	along with this program.  If not, see <http://www.gnu.org/licenses/>.

class TinymceHelper extends Helper{

	//helpers a emplear
	public $helpers = array('Html');

	//Path CSS Custom
	public $css_custom = "/css/main/style_elements.css";

	//Path file manager
	public $file_manager;

	//Idiomas del editor
	public $languages_editor = array('eng' => 'en', 'spa'=>'es');

	//Idiomas elegido
	public $language_selected;

	//opciones por defecto
	public $options = array("id" => null,
							"width" => "400",
							"height" => "150",
							"theme" => "simple",
							"language" => "es",
							"setup" => null
							);


	function beforeRender(){

		//Ruta del gestor de súbida de ficheros
		$this->file_manager = $this->Html->base.DS.'js'.DS.'tinymce'.DS.'plugins'.DS.'ajaxfilemanager'.DS.'ajaxfilemanager.php'; 
		
		//Ruta del fichero css
		$this->css_custom = $this->Html->base.DS.'theme'.DS.$this->Html->theme.$this->css_custom; 		

		//Idioma editor
		$this->language_selected = $this->languages_editor[Configure::read('Config.language')];

	}

	
	/**
	 * Coloca las opciones elegidas para el editor
	 *
	 * @param array $options array con las opciones
	 *
	 * @return void
	 */
	function load($options) {
	
		if(!empty($options) && is_array($options) && isset($options['id'])){

			$this->options = array_merge($this->options, $options);

			//cargo la librería javascript
		    echo $this->Html->script('/js/tinymce/tiny_mce');

			//Lanzo tinymce
			$output = '<script language="javascript" type="text/javascript">';
			$output .= 'tinyMCE.init({';
			$output .= $this->_setSettings($this->options);
			$output .= '});';
			if($options['theme'] == 'advanced'){
				$output .= $this->_managerFiles($this->file_manager);
			}
			$output .= '</script>';

			echo $output;		
		}
	}


	/**
	 * Coloca las opciones elegidas para el editor
	 *
	 * @param array $options array con las opciones
	 * @return $output
	 */
	private function _setSettings($options){

		$output = 'mode : "exact",
					elements : "'.$options['id'].'",
					width : "'.$options['width'].'",
					height : "'.$options['height'].'",
					theme : "'.$options['theme'].'",
					language : "'.$this->language_selected.'",';

		//Podemos realizar llamadas a funciones javascript desde aquí
		if(!empty($options['setup']))
			$output .= 'setup : '.$options['setup'].',';

		//Solo si se trata del módelo advanced, carga el resto de utilidades
		if($options['theme'] == 'advanced'){
			$output .= 'content_css : "'.$this->css_custom.'",
					plugins : "advimage,advlink,media,contextmenu,fullscreen",
					theme_advanced_buttons1_add : "fontselect,fontsizeselect",
					theme_advanced_buttons2_add : "separator,forecolor,backcolor,liststyle,media",
					theme_advanced_buttons2_add_before: "cut,copy,separator",
					theme_advanced_buttons3_add_before : "",
					theme_advanced_buttons3_add : "fullscreen",
					theme_advanced_toolbar_location : "top",
					theme_advanced_toolbar_align : "left",
					extended_valid_elements : "hr[class|width|size|noshade]",
					file_browser_callback : "ajaxfilemanager",
					paste_use_dialog : true,
					theme_advanced_resizing : true,
					theme_advanced_resize_horizontal : true,
					apply_source_formatting : true,
					force_br_newlines : true,
					force_p_newlines : false,
					relative_urls : false' ;
		}

		return $output;

	}

	/**
	 * Gestor de las imagenes
	 *
	 * @param string $url_manager Url del gestor
	 * @return $output
	 */
	private function _managerFiles($url_manager){

		return 'function ajaxfilemanager(field_name, url, type, win) {
					var ajaxfilemanagerurl = "'.$url_manager.'";
					switch (type) {

						case "image":
						break;

						case "media":
						break;

						case "flash": 
						break;

						case "file":
						break;

						default:
						return false;

					}
		
			        tinyMCE.activeEditor.windowManager.open(
					{
			            url: "'.$url_manager.'",
			            width: 782,
			            height: 440,
			            inline : "yes",
			            close_previous : "no"
			        },
					{
			            window : win,
			            input : field_name
			        });
				}';
	}

}
?> 
